package com.hjxr.sysadmin.zsj.service.impl;

import java.util.List;

import com.hjxr.framework.config.ServerConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hjxr.sysadmin.zsj.mapper.CallonMapper;
import com.hjxr.sysadmin.zsj.domain.Callon;
import com.hjxr.sysadmin.zsj.service.ICallonService;

/**
 * 拜访信息Service业务层处理
 *
 * @author zc
 * @date 2021-03-22
 */
@Service
public class CallonServiceImpl implements ICallonService
{
    @Autowired
    private CallonMapper callonMapper;

    @Autowired
    private ServerConfig serverConfig;

    /**
     * 查询拜访信息
     *
     * @param callonId 拜访信息ID
     * @return 拜访信息
     */
    @Override
    public Callon selectCallonById(Long callonId)
    {
        Callon callon = callonMapper.selectCallonById(callonId);
        if (callon != null && callon.getCallonAttachment() != null) {
            callon.setRealAttachment(serverConfig.getUrl() + callon.getCallonAttachment());
        }
        if (callon != null && callon.getVisitorName() != null) {
            callon.setVisitorNames(callon.getVisitorName().split(","));
        }
        if (callon != null && callon.getVisitedPerson() != null) {
            callon.setVisitedPersons(callon.getVisitedPerson().split(","));
        }
        return callon;
    }

    /**
     * 查询拜访信息列表
     *
     * @param callon 拜访信息
     * @return 拜访信息
     */
    @Override
    public List<Callon> selectCallonList(Callon callon)
    {
        List<Callon> list = callonMapper.selectCallonList(callon);
        for (Callon callon1 : list) {
            if (callon1 != null && callon1.getCallonAttachment() != null) {
                callon1.setRealAttachment(serverConfig.getUrl() + callon1.getCallonAttachment());
            }
        }
        return list;
    }

    /**
     * 新增拜访信息
     *
     * @param callon 拜访信息
     * @return 结果
     */
    @Override
    public int insertCallon(Callon callon)
    {
        if (callon.getVisitorNames() != null) {
            String vname = "";
            for (String s : callon.getVisitorNames()) {
                vname += (s + ",");
            }
            callon.setVisitorName(vname);
        }
        if (callon.getVisitedPersons() != null) {
            String vperson = "";
            for (String s : callon.getVisitedPersons()) {
                vperson += (s + ",");
            }
            callon.setVisitedPerson(vperson);
        }
        return callonMapper.insertCallon(callon);
    }

    /**
     * 修改拜访信息
     *
     * @param callon 拜访信息
     * @return 结果
     */
    @Override
    public int updateCallon(Callon callon)
    {
        if (callon.getVisitorNames() != null) {
            String vname = "";
            for (String s : callon.getVisitorNames()) {
                vname += (s + ",");
            }
            callon.setVisitorName(vname);
        }
        if (callon.getVisitedPersons() != null) {
            String vperson = "";
            for (String s : callon.getVisitedPersons()) {
                vperson += (s + ",");
            }
            callon.setVisitedPerson(vperson);
        }
        return callonMapper.updateCallon(callon);
    }

    /**
     * 批量删除拜访信息
     *
     * @param callonIds 需要删除的拜访信息ID
     * @return 结果
     */
    @Override
    public int deleteCallonByIds(Long[] callonIds)
    {
        return callonMapper.deleteCallonByIds(callonIds);
    }

    /**
     * 删除拜访信息信息
     *
     * @param callonId 拜访信息ID
     * @return 结果
     */
    @Override
    public int deleteCallonById(Long callonId)
    {
        return callonMapper.deleteCallonById(callonId);
    }
}
